/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gee.util;

import java.io.InputStream;
import java.util.Hashtable;

/*
 * 配置类
 * 用于读取配置文件便于移植，占时不用
 */

/**
 * 
 * @author CoCo
 */
public class ConfigUtil {

	private Hashtable hashtable; // 系统的一个词典

	public void loadTxt(String nameP) {

		InputStream is = getClass().getResourceAsStream(nameP);
		if (is == null) {
			System.out.println(nameP + "没有找到配置文件");
			return;
		}
		try {
			String line;
			hashtable = new Hashtable();
			while (true) {
				line = getLine(is);
				if (line == null) {
					break;
				}
				if (line.length() > 2 & line.charAt(0) != '/') {
					int split = line.indexOf("=");
					if (split != -1) {
						String key = line.substring(0, split);
						String value = line.substring(split + 1, line.length());
						key = key.trim();
						value = value.trim();
						hashtable.put(key, value);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private String getLine(InputStream is) {
		int nextByte = 0;
		byte[] byteArray = new byte[1024];
		int count = 0;
		while (true) {
			try {
				nextByte = is.read();
				byteArray[count] = (byte) nextByte;
				++count;
			} catch (Exception e) {
				e.printStackTrace();
			}

			if (nextByte == '\n') {
				try {
					return new String(byteArray, 0, count, "UTF-8");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (nextByte == -1) {
				return null;
			}
		}

	}

	public int getIntVal(String key, int defval) {
		String val = (String) hashtable.get(key);
		if (val == null) {
			return defval;
		}
		return Integer.parseInt(val);
	}

	public String getStringVal(String key, String defval) {
		String val = (String) hashtable.get(key);
		if (val == null) {
			return defval;
		}
		return val;
	}
}
